import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus';
const daoName = "IndiTbYdzb";
const ButtonGroup = Button.Group;
const columns = [
    { title: '年份', dataIndex: 'NF', key: 'NF', width: 100, sorter: true, dispaly: true, filter: true, export: false },
    { title: '指标', width: 140, sorter: true, key: 'LXID', caption: "ConfIndiQhrw.RWMC", render: data => data.ConfIndiQhrw.RWMC, dispaly: true, filter: false, export: true },
    { title: '一月', width: 100, sorter: true, dataIndex: 'M1', key: 'M1', dispaly: true, filter: false, export: true },
    { title: '二月', width: 100, sorter: true, key: 'M2', dataIndex: 'M2', dispaly: true, filter: false, export: true },
    { title: '三月', width: 100, sorter: true, key: 'M3', dataIndex: "M3", dispaly: true, filter: false, export: true },
    { title: '四月', width: 100, sorter: true, key: 'M4', dataIndex: "M4", dispaly: true, filter: false, export: true },
    { title: '五月', width: 100, sorter: true, dataIndex: 'M5', key: 'M5', dispaly: true, filter: false, export: true },
    { title: '六月', width: 100, sorter: true, key: 'M6', dataIndex: 'M6', dispaly: true, filter: false, export: true },
    { title: '七月', width: 100, sorter: true, key: 'M7', dataIndex: "M7", dispaly: true, filter: false, export: true },
    { title: '八月', width: 100, sorter: true, key: 'M8', dataIndex: "M8", dispaly: true, filter: false, export: true },
    { title: '九月', width: 100, sorter: true, dataIndex: 'M9', key: 'M9', dispaly: true, filter: false, export: true },
    { title: '十月', width: 100, sorter: true, key: 'M10', dataIndex: 'M10', dispaly: true, filter: false, export: true },
    { title: '十一月', width: 100, sorter: true, key: 'M11', dataIndex: "M11", dispaly: true, filter: false, export: true },
    { title: '十二月', width: 100, sorter: true, key: 'M12', dataIndex: "M12", dispaly: true, filter: false, export: true },
];
const controls = [
    {
        name: 'LXID', required: true, type: 'search', label: '指标', grid: 10 / 24,
        fetch: { daoName: "ConfIndiQhrw", searchColumns: ['RWMC'], optionTpl: 'RWMC', valueTpl: "ID" }
    },
    { name: 'M1', required: true, type: 'number', dataType: "number", label: '一月', min: 1, max: 50, grid: 10 / 24 },
    { name: 'M2', required: true, type: 'number', dataType: "number", label: '二月', min: 1, max: 50, grid: 10 / 24 },
    { name: 'M3', required: true, type: 'number', dataType: "number", label: '三月', min: 1, max: 50, grid: 10 / 24 },
    { name: 'M4', required: true, type: 'number', dataType: "number", label: '四月', min: 1, max: 50, grid: 10 / 24 },
    { name: 'M5', required: true, type: 'number', dataType: "number", label: '五月', min: 1, max: 50, grid: 10 / 24 },
    { name: 'M6', required: true, type: 'number', dataType: "number", label: '六月', min: 1, max: 50, grid: 10 / 24 },
    { name: 'M7', required: true, type: 'number', dataType: "number", label: '七月', min: 1, max: 50, grid: 10 / 24 },
    { name: 'M8', required: true, type: 'number', dataType: "number", label: '八月', min: 1, max: 50, grid: 10 / 24 },
    { name: 'M9', required: true, type: 'number', dataType: "number", label: '九月', min: 1, max: 50, grid: 10 / 24 },
    { name: 'M10', required: true, type: 'number', dataType: "number", label: '十月', min: 1, max: 50, grid: 10 / 24 },
    { name: 'M11', required: true, type: 'number', dataType: "number", label: '十一月', min: 1, max: 50, grid: 10 / 24 },
    { name: 'M12', required: true, type: 'number', dataType: "number", label: '十二月', min: 1, max: 50, grid: 10 / 24 },
    { name: 'NF', required: true, type: 'text', label: '年份', min: 1, max: 50, grid: 10 / 24 },
]
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoName);
class IndiTbYdzbForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20 }
        });
        this.loadOptionData();
    }
    componentWillReceiveProps(nextProps) {
        const { showImport } = this.props
        if (!nextProps.showImport && showImport) {
            this.props.dispatch({
                type: `${daoName}/fetch`,
                payload: { daoName, page: 1, pageSize: 20 }
            });
        }
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    showImportHandler() {
        this.props.dispatch({
            type: `${daoName}_DataImport/setDisplay`,
            payload: { display: true },
        });
    }
    DownTmportHandler() {
        let columnHeaders = []
        columns.map((c, j) => {
            if (!c.export) {
                return
            }
            const name = c.dataIndex ? c.dataIndex : c.caption
            let header = {
                Name: name,
                Caption: c.title,
                Width: c.width
            }
            columnHeaders.push(header)
        })
        let form = document.createElement('form');//定义一个form表单
        let whereItems = [];
        whereItems.push({
            FieldName: "NF",
            Operator: "=",
            FieldValues: "2017"
        })
        const whereCauses = []
        whereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        const orderItems = []
        form.style = "display:none";
        form.target = "_blank";
        form.method = "post";
        form.action = "/api/ExportXlsx/" + daoName;
        let input1 = document.createElement("input");
        input1.type = "hidden";
        input1.name = "WhereCauses";
        input1.value = JSON.stringify(whereCauses);
        let input2 = document.createElement("input");
        input2.type = "hidden";
        input2.name = "OrderItems";
        input2.value = JSON.stringify(orderItems)
        let input3 = document.createElement("input");
        input3.type = "hidden";
        input3.name = "ColumnHeaders";
        input3.value = JSON.stringify(columnHeaders);
        let input4 = document.createElement("input");
        input4.type = "hidden";
        input4.name = "PermID";
        input4.value = "" + this.props.permID;
        let input5 = document.createElement("input");
        input5.type = "hidden";
        input5.name = "UserID";
        input5.value = "" + this.props.userID;
        document.body.appendChild(form);//将表单放置在web中
        form.appendChild(input1);
        form.appendChild(input2);
        form.appendChild(input3);
        form.appendChild(input4);
        form.appendChild(input5);
        form.submit();
        document.body.removeChild(form)
    }
    render() {
        let { showEdit, record, showImport, permID, popedomTypes } = this.props
        const pTypes = popedomTypes[permID]
        let data = null
        if (pTypes.indexOf("2") !== -1) {
            data = <ButtonGroup style={{ zIndex: 0 }}>
                <Button onClick={this.showImportHandler.bind(this)}>导入</Button>
                <Button onClick={this.DownTmportHandler.bind(this)}>下载模板</Button>
            </ButtonGroup>
        }
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        {data}
                    </div>
                    <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
                    <DataImport title="序时进度-月" importName="IndiTbYdzb" daoName="IndiTbYdzb" keys={["NF"]} paramNames={["NF"]} importMode="DeleteAfterInsert" controls={controls} />
                </div>
            </div>
        )
    }
}

function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes } = state.menus;
    const { display } = state[`${daoName}_DataImport`];
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record,
        showImport: display
    }
}
IndiTbYdzbForm = connect(mapStateToProps)(IndiTbYdzbForm)
export default IndiTbYdzbForm;

let render = (props) => React.createElement(IndiTbYdzbForm, props);
let f = new Factory()
f.set("IndiTbYdzbForm", render)
